﻿using System;

namespace h1
{
    public class MyStack<T>
    {
        public T[] Items;
        private int _top;
        private int _capacity;
        public MyStack(int i)
        {
            _capacity = i;
            Items = new T[_capacity];
            _top = -1;
        }
        public void InitStack()
        {
            Items = new T[_capacity];
            _top = -1;
        }
        public void ClearStack()
        {
            _top = -1;
        }
        public bool Push(T item)
        {
            if (_top == _capacity - 1) { return false; }
            _top++;
            Items[_top] = item;
            return true;
        }
        public T Pop()
        {
            if (_top == -1) { return default(T); }
            T topItem = Items[_top];
            _top--;
            return topItem;
        }
    }
    internal class Program
    {
        public static void Main(string[] args)
        {
            Console.WriteLine("=== 整数栈测试 ===");
            MyStack<int> intStack = new MyStack<int>(3);
            intStack.InitStack();
            Console.WriteLine("入栈 100:" + (intStack.Push(100) ? "成功" : "失败"));
            Console.WriteLine("入栈 200:" + (intStack.Push(200) ? "成功" : "失败"));
            Console.WriteLine("入栈 300:" + (intStack.Push(300) ? "成功" : "失败"));
            Console.WriteLine("入栈 400:" + (intStack.Push(400) ? "成功" : "失败"));
            Console.WriteLine("出栈元素:" + intStack.Pop());
            Console.WriteLine("出栈元素:" + intStack.Pop());
            intStack.ClearStack(); Console.WriteLine("清空栈后出栈:" + intStack.Pop());

            Console.WriteLine("\n=== 字符串栈测试 ===");
            MyStack<string> strStack = new MyStack<string>(2); strStack.InitStack();
            Console.WriteLine("入栈 'First':" + (strStack.Push("First") ? "成功" : "失败"));
            Console.WriteLine("入栈 'Second':" + (strStack.Push("Second") ? "成功" : "失败"));
            Console.WriteLine("出栈元素:" + strStack.Pop());
            Console.WriteLine("入栈 'Third':" + (strStack.Push("Third") ? "成功" : "失败"));
        }
    }
}